const express = require("express");

const router = express.Router();

const config = require("../../config/config");
const { md5, getUnix } = require("../../model/tools");
const ManagerModel = require("../../model/managerModel");

router.get("/", async (req, res) => {
    // 获取管理员数据
    let result = await ManagerModel.find({});
    console.log(`result`, result);
    // res.render("admin/manager/index.html", { list: result });
    res.render("admin/manager", { list: result });
})

router.get("/add", (req, res) => {
    res.render("admin/manager/add.html");
});

router.post("/doAdd", async (req, res) => {
    // 1. 获取表单提交的数据
    let username = req.body.username;
    let password = req.body.password;
    let confirmPassword = req.body.confirmPassword;
    let email = req.body.email;
    let mobile = req.body.mobile;
    let status = req.body.status;

    // 2. 验证数据是否合法
    if(username == "") {
        res.render("admin/public/error.html", {
            "redirectUrl": `/${config.adminPath}/manager/add`,
            "message": "用户名密码不能为空"
        });
        return;
    }

    if(password.length < 6) {
        res.render("admin/public/error.html", {
            "redirectUrl": `/${config.adminPath}/manager/add`,
            "message": "密码长度不能小于 6 位"
        });
        return;
    }

    if(password != confirmPassword) {
        res.render("admin/public/error.html", {
            "redirectUrl": `/${config.adminPath}/manager/add`,
            "message": "密码和确认密码不一致"
        });
        return;
    }

    // 3. 判断数据库里面有没有我们要增加的用户
    let result = await ManagerModel.find({ username });
    if(result.length > 0) {
        res.render("admin/public/error.html", {
            "redirectUrl": `/${config.adminPath}/manager/add`,
            "message": "当前用户已经存在，请换一个用户"
        });
        return;
    } else {
        // 4. 执行增加操作
        const addResult = new ManagerModel({
            username,
            password: md5(password),
            mobile,
            email,
            add_time: getUnix(),
            status
        })
        await addResult.save();
        res.redirect(`/${config.adminPath}/manager`);
    }
});

router.get("/edit", async (req, res) => {
    // 获取要修改数据的id
    const id = req.query.id;
    const result = await ManagerModel.find({ "_id": id });
    if(result.length > 0) {
        res.render("admin/manager/edit.html", {
            list: result[0]
        });
    } else {
        res.redirect(`/${config.adminPath}/manager`);
    }
});

router.post("/doEdit", async (req, res) => {
    // 1. 获取表单提交的数据
    let id = req.body.id;
    let password = req.body.password;
    let confirmPassword = req.body.confirmPassword;
    let email = req.body.email;
    let mobile = req.body.mobile;
    let status = req.body.status;

    if(password.length > 0) {
        // 修改密码
        if(password.length < 6) {
            res.render("admin/public/error.html", {
                "redirectUrl": `/${config.adminPath}/manager/edit?id=${id}`,
                "message": "密码长度不能小于 6 位"
            });
            return;
        }

        if(password != confirmPassword) {
            res.render("admin/public/error.html", {
                "redirectUrl": `/${config.adminPath}/manager/edit?id=${id}`,
                "message": "密码和确认密码不一致"
            });
            return;
        }
        await ManagerModel.updateOne({ _id: id }, { password: md5(password), email, mobile, status });
    } else {
        // 不修改密码，只修改其他信息
        await ManagerModel.updateOne({ _id: id }, { email, mobile, status });
    }
    res.redirect(`/${config.adminPath}/manager`);
});

router.get("/delete", async (req, res) => {
    // 获取要修改数据的id
    const id = req.query.id;
    const result = await ManagerModel.deleteOne({ "_id": id });
    console.log(`result`, result);
    res.render("admin/public/success.html", {
        "redirectUrl": `/${config.adminPath}/manager`,
        "message": "删除数据成功"
    });
})

module.exports = router